package com.stylefeng.guns.modular.vpn_exclusive_line_manage.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.VpnPackageInfo;
import com.stylefeng.guns.modular.vpn_exclusive_line_manage.dto.PackageDTO;
import com.stylefeng.guns.modular.vpn_exclusive_line_manage.entity.ExclusiveLineDO;
import com.stylefeng.guns.modular.vpn_exclusive_line_manage.entity.VpnServerDO;
import com.stylefeng.guns.modular.vpn_exclusive_line_manage.service.IVpnExclusiveLineService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 专属线路管理控制器
 */
@Controller
@RequestMapping("/vpnExclusive")
public class VpnExclusiveLineManageController extends BaseController {

    private String PREFIX = "/vpnExclusiveLine/";

    @Autowired
    private IVpnExclusiveLineService lineService;

    /**
     * 跳转到专属线路管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "vpnExclusiveLine.html";
    }

    /**
     * 跳转到添加专属线路管理
     */
    @RequestMapping("/vpnExclusiveLine_add")
    public String vpnExclusiveLineAdd() {
        return PREFIX + "vpnExclusiveLine_add.html";
    }


    @RequestMapping("/vpnExclusiveLine_update/{serverId}")
    public String vpnExclusiveLineUpdate(@PathVariable Integer serverId, Model model) {
        ExclusiveLineDO lineDO = lineService.findExclusiveLinePackage(serverId);
        model.addAttribute("item",lineDO);
        LogObjectHolder.me().set(lineDO);
        return PREFIX + "vpnExclusiveLine_update.html";
    }

    /**
     * 获取专属线路管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",defaultValue = "20") Integer limit) {
        return lineService.list(page,limit);
    }

    /**
     * 新增专属线路管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestParam("serverId") Integer serverId,
                      @RequestParam("username") String username,
                      @RequestParam("availableDays") Integer availableDays) {
        if (serverId == 0
                || StringUtils.isEmpty(username)){
            throw new GunsException(BizExceptionEnum.EXCLUSIVE_PARAMS_ERROR);
        }
        PackageDTO dto = new PackageDTO();
        dto.setUserName(username);
        dto.setServerId(serverId);
        dto.setAvailableDays(availableDays);
        lineService.addPackageOfUser(dto);
        return SUCCESS_TIP;
    }

    /**
     * 删除专属线路管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam("serverId") String serverId,
                         @RequestParam("userId") String userId) {
        lineService.deletePackage(serverId,userId);
        return SUCCESS_TIP;
    }

    /**
     * 修改专属线路下的用户名单
     * @param serverId
     * @param userNames
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@RequestParam("serverId") Integer serverId,
                         @RequestParam("userNames") String userNames) {
        if (serverId == 0){
            throw new GunsException(BizExceptionEnum.EXCLUSIVE_PARAMS_ERROR);
        }
        lineService.update(serverId,userNames);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/serverList")
    @ResponseBody
    public Object serverList() {
        return lineService.serverList();
    }

}
